﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;

namespace NorthwindClient
{
    /// <summary>
    /// Interaction logic for TestCavusAdding.xaml
    /// </summary>
    public partial class TestCanvasAdding : Page
    {
        public TestCanvasAdding()
        {
            InitializeComponent();
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new
                Uri("Home.xaml", UriKind.Relative));
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            DateTime loadStartTime = DateTime.Now;
            List<CellControl> cellControls = CellControl.GetCellControls();

            int rowNumber = cellControls.Max(cc => cc.CordY + cc.Height);
            int colNumber = cellControls.Max(cc => cc.CordX + cc.Width);
            double rowUnitPixel = (this.cavContainer.Height - 20) / (rowNumber * 11.0 - 1.0);
            double colUnitPixel = (this.cavContainer.Width - 20) / (colNumber * 11.0 - 1.0);

            for (int i = 0; i < cellControls.Count; i++)
            {
                CellControl cellControl = cellControls[i];
                Button btn = new Button()
                {
                    Name = "btn" + 1,
                    Width = (cellControl.Width * 11 - 1) * colUnitPixel,
                    Height = (cellControl.Height * 11 - 1) * rowUnitPixel
                };
                this.cavContainer.Children.Add(btn);
                Canvas.SetLeft(btn, (cellControl.CordX * 11) * colUnitPixel + 10);
                Canvas.SetTop(btn, (cellControl.CordY * 11) * rowUnitPixel + 10);
            }
            TimeSpan loadTime = DateTime.Now - loadStartTime;
            string title = lblTitle.Content.ToString();
            lblTitle.Content = title + "  Takes " + loadTime.TotalSeconds + "s to load";
        }
    }
}
